﻿using CloudDcPc.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc.FuckDlg
{
    /// <summary>
    /// TogetherEditDlg.xaml 的交互逻辑
    /// </summary>
    public partial class TogetherEditDlg : Window
    {
        private Together tg = null;
        private string preTgName = "";

        private ObservableCollection<MyMenu> list_left = new ObservableCollection<MyMenu>();
        private ObservableCollection<MyMenu> list_right = new ObservableCollection<MyMenu>();

        public TogetherEditDlg(Together tg)
        {
            InitializeComponent();

            List<string> list_food_type = new List<string>();
            list_food_type = App.MyMenuBean.GetAllType();
            cb_food_type.ItemsSource = list_food_type;

            this.tg = tg;
            preTgName = tg.Name;
            main_grid.DataContext = tg;
            list_left = App.TogetherBean.GetNotInMyMenu();
            list_right = App.TogetherBean.GetInMyMenu(tg.Name);
            list_left_xml.ItemsSource = list_left;
            list_right_xml.ItemsSource = list_right;
            refresh_sum();

        }
        private void cb_food_type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            list_left = App.MyMenuBean.GetMyMenusByType(cb_food_type.SelectedItem + "");
            list_left_xml.ItemsSource = list_left;
        }
        private void btn_2right_Click(object sender, RoutedEventArgs e)
        {
            foreach (MyMenu item in list_left_xml.SelectedItems)
            {
                if (list_right.IndexOf(item) < 0)
                    list_right.Add(item);
            }
            list_left_xml.SelectedIndex = -1;
            refresh_sum();
        }

        private void btn_2left_Click(object sender, RoutedEventArgs e)
        {
            List<MyMenu> list = new List<MyMenu>();
            //遍历过程中不能修改故...
            foreach (MyMenu item in list_right_xml.SelectedItems)
            {
                list.Add(item);
            }
            foreach (MyMenu item in list)
            {
                list_right.Remove(item);
            }
            list_right_xml.SelectedIndex = -1;
            refresh_sum();
        }

        public void refresh_sum()
        {
            float sum = 0;
            foreach (MyMenu item in list_right)
            {
                try
                {
                    if (item.CountInTg == null)
                        return;
                    sum += float.Parse(item.CountInTg) * item.Price;
                }
                catch
                {
                    MessageBox.Show("数量只能为数字！");
                    txt_true_sum.Text = "0";
                }
            }
            txt_true_sum.Text = sum + "";
        }


        private void list_right_xml_KeyUp_1(object sender, KeyEventArgs e)
        {
            refresh_sum();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string name = txt_name.Text;
            float tg_sum;
            try
            {
                tg_sum = float.Parse(tb_tg_sum.Text);
            }
            catch
            {
                MessageBox.Show("套餐价填写有误！");
                return;
            }
            if (name == "")
            {
                MessageBox.Show("请填写套餐名称！");
                return;
            }

            foreach (MyMenu item in list_right)
            {
                if (item.CountInTg == null)
                {
                    MessageBox.Show("请填写"+item.Food_name+"的数量！");
                    return;
                }
            }


            BindingExpression bind = txt_name.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = tb_tg_sum.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();

            tg.Name = name;
            tg.Price = tg_sum;
            if (preTgName != tg.Name)
                if (App.TogetherBean.HasTogether(tg.Name))
                {
                    MessageBox.Show("该套餐名已经存在！");
                    return;
                }

            if (App.TogetherBean.UpdateTg(preTgName, tg, list_right))
            {
                MessageBox.Show("修改成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败！");
                return;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string messageBoxText = "确认删除该套餐吗？";
            string caption = "删除";
            MessageBoxButton button = MessageBoxButton.YesNo;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == MessageBoxResult.Yes)
            {
                if (App.TogetherBean.DelTogetherById(tg.Id))
                {
                    MessageBox.Show("删除成功！");
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                    return;
                }
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
